﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Upreader.Usenet.Nntp;
using System.IO;
using Upreader.Engine;
using Upreader.Application;
using Upreader.Application.Default;
using Upreader.Application.Model;
using Upreader.Infrastructure.ProtoBuf;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Net;

namespace UpreaderConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // install trace listener for reporting
            Trace.Listeners.Add(new ConsoleTraceListener());

            DataContext context = new DataContext();
            IApplicationService service = new ApplicationService(() => new ProtoBufDomainContext(context));
            var engine = new DownloadEngine(service, new DownloadEngineConfiguration()
            {
                SegmentsInMemoryCache = 4
            });

            service.AddServer(new Server()
            {
                Title = "Test server",
                Address = "newsreader109.eweka.nl",
                Port = 563,
                IsSecuredConnection = true,
                UserName = "55099",
                Password = "h649cx",
                IsAuthenticationRequired = true,
                ConnectionCount = 1,
                Enabled = true
            });

            CancellationTokenSource exitTokenSource = new CancellationTokenSource();

            Task downloadTask = null;

            while (true)
            {
                string line = Console.ReadLine();

                if (string.Compare(line, "q", true) == 0)
                {
                    Console.WriteLine("Shutting down...");

                    exitTokenSource.Cancel();

                    if (downloadTask != null)
                        downloadTask.ContinueWith(result =>
                        {
                            if (result.IsFaulted)
                                Console.WriteLine(result.Exception.Message);
                            else
                            {
                                downloadTask = null;    
                                Console.WriteLine("Download Task closed");
                            }
                        });
    
                }
                else if (string.Compare(line, "c", true) == 0)
                {
                    if (downloadTask != null)
                        Console.WriteLine("Download Task is already running");
                    else
                    {
                        Console.WriteLine("Start downloading");
                        downloadTask = engine.StartDownloadTask(exitTokenSource.Token);
                    }
                }
                else if (string.Compare(line, "l", true) == 0)
                {
                    Console.WriteLine("Loading sample nzb");

                    string url = @"C:\Users\Koen\Downloads\uuencode.nzb";
                    WebClient client = new WebClient();
                    byte[] data = client.DownloadData(url);

                    service.ParseNzbStream(new MemoryStream(data), "uuencode.nzb");

                    Console.WriteLine("Loaded sample nzb");
                }
            }
        }
    }
}
